package com.uchiha.nova.mainService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uchiha.nova.mainService.mapper.BtnInfoMapper;
import com.uchiha.nova.mainService.mapper.RoleBtnMapper;
import com.uchiha.nova.mainService.service.IBtnInfoService;
import com.uchiha.nova.core.model.entity.BtnInfoEntity;
import com.uchiha.nova.core.model.entity.RoleBtn;
import com.uchiha.nova.core.model.entity.RoleEntity;
import com.uchiha.nova.core.tool.support.BaseServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 按钮信息 服务实现类
 *
 * @author Chill
 */
@Service
public class BtnInfoServiceImpl extends BaseServiceImpl<BtnInfoMapper, BtnInfoEntity> implements IBtnInfoService {
    @Override
    public boolean deletePhysics(List<Long> ids) {
        ids.forEach(item-> this.baseMapper.deletePhysics(item));
        return true;
    }

    @Resource
    RoleBtnMapper roleBtnMapper;
    @Override
    public List<String> getBtnByRoles(List<RoleEntity> roles) {
        List<RoleBtn> allRoleBtns = new ArrayList<>();
        roles.forEach(item->{
            List<RoleBtn> roleBtns = roleBtnMapper.selectList(new QueryWrapper<RoleBtn>().eq("role_id",item.getId()));
            allRoleBtns.addAll(roleBtns);
        });
        List<RoleBtn> finalRoleBtns = allRoleBtns.stream().distinct().collect(Collectors.toList());
        List<String> list = finalRoleBtns.stream().map(i->{
            BtnInfoEntity btnInfo = this.getById(i.getBtnId());
            return btnInfo.getCode();
        }).collect(Collectors.toList());
        return list;
    }
}
